#!/usr/bin/env python
#coding=utf-8

from __future__ import division
import time
import sys
from define import BASE_DIR
sys.path.append(BASE_DIR)

#import pymongo
from lib.abstract_cron import abstract_cron
from lib.func_ext import str2sec,sec2str,ksort

#需要修改
class api_cat_report_day(abstract_cron) :

    #需要修改
    #noLock = True   #是否生成脚本锁文件
    selectTable = 'sdb_statistics_api_day'
    insertTable = 'sdb_statistics_api_cat_report_day'

    def __init__(self) :
        #需要修改
        super(api_cat_report_day,self).__init__(__file__)

    def boot(self,timestamp={}) :
#        self.preBoot()

        row = self.getData(timestamp)
        if row :
            for k,v in row.iteritems() :
                if not v.get('date') :
                    v['date'] = sec2str('%Y%m%d',time.time()-1*24*3600)
                if not v.get('calltime') :
                    v['calltime'] = int(time.time()-1*24*3600)
                #总成功率
                self.figureRatio(v,'api_visit_num_total','api_fail_num_total','api_succ_ratio_total')
                #日成功率
                self.figureRatio(v,'api_visit_num','api_fail_num','api_succ_ratio')
                #数据过滤
                self.filterItems(v)

            #批量插入
            self.doBatchExec()

        self.sufBoot()
        print 'ok'

    def getData(self,timestamp={}) :
        catData = {}
        timeFilter = self.getTimeFilter(timestamp)
        for k,v in timeFilter.iteritems() :
            if k == 'total' :
                cols  = ' m.`app_cat` AS api_cat,SUM(d.`sum`) AS api_visit_num_total,'
                cols += ' (SUM(d.`sum`)-SUM(d.`sum_succ`)) AS api_fail_num_total'
            else :
                cols  = ' m.`app_cat` AS api_cat,SUM(d.`sum`) AS api_visit_num,'
                cols += ' (SUM(d.`sum`)-SUM(d.`sum_succ`)) AS api_fail_num'
            sql  = ' SELECT '+cols
            sql += ' FROM `sdb_statistics_api_day` AS d '
            sql += ' LEFT JOIN sdb_statistics_api_method AS m ON m.`app_instance_id` = d.`api`'
            sql += ' WHERE '+self.db._filter(v,'d')
            sql += ' GROUP BY m.`app_cat`'


            row = self.db.select(sql)
            if row :
                self.writeSql(sql,'succ','select')
                for v in row :
                    api_cat = v.get('api_cat')
                    if api_cat :
                        if api_cat not in catData :
                            catData[api_cat] = {}
                        catData[api_cat].update(v)
            else :
                self.writeSql(sql,'fail','select')
        return catData

    def getTimeFilter(self,timestamp={}) :
        keyFilter = {}
        date = timestamp.get('day')
        keyFilter['day'] = {'date':(date and date or int(sec2str('%Y%m%d',time.time()-1*24*3600)))}
        calltime = timestamp.get('total')
        keyFilter['total'] = {'call_time|lt':(calltime and calltime or str2sec(sec2str('%Y%m%d'),'%Y%m%d'))}
        return keyFilter

    def insertKeys(self) :
        return ['api_cat',
                'api_visit_num','api_visit_num_total',
                'api_fail_num','api_fail_num_total',
                'api_succ_ratio','api_succ_ratio_total',
                'date','calltime',
                ]


#需要修改
if __name__ == '__main__' :
    obj = api_cat_report_day()
    obj.boot()
    #timestamp = 20100314
    #obj.boot(timestamp)